package com.saga.energy.app;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.saga.energy.bean.EnergyHistory;
import com.saga.energy.sink.Energy_15_fjd_history_sink;
import com.saga.energy.sink.Energy_15_history_sink;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.flink.util.Collector;

import java.util.Properties;

public class KafkaToMysql {

    public static void main(String[] args) throws Exception {


        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "hadoop01:9092,hadoop02:9092,hadoop03:9092");
        properties.setProperty("group.id", "energy-02");
        properties.setProperty("auto.offset.reset", "earliest");

        DataStreamSource<String> energy_15_min = env.addSource(new FlinkKafkaConsumer<>("ENERGY_15_MIN", new SimpleStringSchema(), properties));
        DataStreamSource<String> energy_15_min_fjd = env.addSource(new FlinkKafkaConsumer<>("ENERGY_15_MIN_FJD", new SimpleStringSchema(), properties));

//        JDBCOutputFormat


        SingleOutputStreamOperator<EnergyHistory> energy_15_minStream = energy_15_min.flatMap(new FlatMapFunction<String, EnergyHistory>() {
            @Override
            public void flatMap(String s, Collector<EnergyHistory> collector) {

                JSONObject jsonObject = JSON.parseObject(s);
                jsonObject.put("dt", jsonObject.getString("data_time").substring(0, 8));
                System.out.println(jsonObject.toJSONString());
                EnergyHistory energyHistory = JSON.parseObject(jsonObject.toJSONString(), EnergyHistory.class);

                collector.collect(energyHistory);
            }
        });

        SingleOutputStreamOperator<EnergyHistory> energy_15_min_fjdStream = energy_15_min_fjd.flatMap(new FlatMapFunction<String, EnergyHistory>() {
            @Override
            public void flatMap(String s, Collector<EnergyHistory> collector) throws Exception {
                JSONObject jsonObject = JSON.parseObject(s);
                jsonObject.put("dt", jsonObject.getString("data_time").substring(0, 8));
                System.out.println(jsonObject.toJSONString());
                EnergyHistory energyHistory = JSON.parseObject(jsonObject.toJSONString(), EnergyHistory.class);

                collector.collect(energyHistory);
            }
        });


//        energy_15_minStream.print();
        energy_15_minStream.addSink(new Energy_15_history_sink());
        energy_15_min_fjdStream.addSink(new Energy_15_fjd_history_sink());

        env.execute("energy_15_min：");
    }
}
